package com.yangtao.prototype.system.log.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yangtao.framework.service.BaseService;
import com.yangtao.prototype.system.dao.LogNoteDAO;
import com.yangtao.prototype.system.dao.SerialNumberDAO;
import com.yangtao.prototype.system.dao.UserDAO;
import com.yangtao.prototype.system.entity.SysUser;
import com.yangtao.prototype.system.log.service.LogModel;
import com.yangtao.prototype.system.log.service.LogService;

/**
 * 
 * @author 杨涛
 * @date 2010-03-24
 */
@Service
public class LogServiceImpl extends BaseService implements LogService {
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private LogNoteDAO lnd;
	@Autowired
	private SerialNumberDAO idGenerator;

	public boolean addUser(String userCode, String password) {
		// Role role = new Role();
		// role.setRoleCode("SYS");
		// role.setRoleName("系统管理员");
		// for (int i = 0; i < 10; i++) {
		// User user = new User();
		// user.setUserCode(userCode + i);
		// user.setPassword(password);
		// user.setRole(role);
		// userDao.save(user);
		// }
		for (int i = 0; i < 200; i++) {
			String number = idGenerator.getNextNumber("001");
			System.out.println(number);
		}
		// throw new RuntimeException("");
		return true;
	}

	public void login(LogModel model) {
		SysUser user = model.getDomain();
		List<SysUser> sysUsers = userDAO.findByProperty("userCode", user
				.getUserCode());
		// 用户不存在
		if (sysUsers == null || sysUsers.size() == 0) {
			model.addError("user.not.exsit");
			return;
		}
		SysUser currentUser = sysUsers.get(0);
		// 密码错误
		if (!currentUser.getPassword().equals(user.getPassword())) {
			model.addError("password.wrong");
			return;
		}
		// 用户过期
		Date today = new Date();
		if (currentUser.getExpireDate() != null
				&& currentUser.getExpireDate().compareTo(today) < 0) {
			model.addError("user.expired");
			return;
		}
		model.setDomain(currentUser);
		// 加载用户菜单
		model.setMenus(userDAO.findMenus(model.getDomain().getId()));
	}

	@Override
	public void getAhoritiedMenus(LogModel model) {
		model.setMenus(userDAO.findMenus(model.getDomain().getId()));
		// if (StringHelper.isEmpty(model.getTopId()))
		// model.setMenus(userDAO.findTopMenu(model.getDomain().getId()));
		// model.setMenus(userDAO.findSubMenus(model.getDomain().getId(), model
		// .getTopId()));
	}

	// @Override
	// public boolean addNewAuth(String menuId, String roleId) {
	// return false;
	// }

	@Override
	public void addLogNote(LogModel model) {
		lnd.save(model.getLn());
	}

	@Override
	public void getOnLineUser(LogModel model) {
		model.setPage(lnd.findOnLineUser(model.getPage()));
	}

	@Override
	public void changeLogNote(LogModel model) {
		lnd.logOut(model.getLogId());
	}

	@Override
	public void findLogs(LogModel model) {
		lnd.findPageByExample(model.getPage(), model.getLn());
	}

}
